﻿/*
This file is under Microsoft Public License (MS-PL)
Copyright Yohann MARTEL.  All rights reserved.
Codeplex: http://amf.codeplex.com
AMF Website: http://www.ymartel.com/aspnet-mobile-framework/
*/
using System;
using System.ComponentModel;
using System.Web.UI;

namespace Mobile.WebControls
{
    public abstract class BaseLink : ThemedControl
    {
        #region Properties
        [Category("Appearance"),
        Bindable(true)]
        public string Url { get; set; }

        [Category("Transition")]
        public DataTransition DataTransition { get; set; }

        [Category("Transition")]
        public DataDirection DataDirection { get; set; }

        [Category("DialogBox")]
        public DialogBox DialogBox { get; set; }

        [Category("Functionality")]
        public bool ForceToReload { get; set; }

        [Category("Functionality")]
        public bool UseAjaxNavigation { get; set; }

        [Category("Functionality")]
        public bool PrefetchData { get; set; }
        #endregion

        #region Contructor
        protected BaseLink()
        {
            UseAjaxNavigation = true; //by default, link use ajax navigation
        }
        #endregion

        #region Render Methods
        protected override void Render(HtmlTextWriter writer)
        {
            if (Visible)
                base.Render(writer);
        }

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            AddAttributesToRender(writer);
            writer.RenderBeginTag(HtmlTextWriterTag.A);
        }

        public override void RenderEndTag(HtmlTextWriter writer)
        {
            writer.RenderEndTag(); // </a>
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            if (!String.IsNullOrEmpty(Url))
            {
                if (ForceToReload)
                {
                    Url = ControlHelper.AddUniqueInUrl(Url);
                }
                writer.AddAttribute(HtmlTextWriterAttribute.Href, Url);
            }
            if (!UseAjaxNavigation)
            {
                writer.AddAttribute(MetadataAttribute.DataAjax, Boolean.FalseString.ToLower());
            }

            if (DataDirection != DataDirection.Default)
            {
                writer.AddAttribute(MetadataAttribute.DataDirection, Enum.GetName(typeof(DataDirection), DataDirection).ToLower());
            }
            if (DataTransition != DataTransition.None)
            {
                writer.AddAttribute(MetadataAttribute.DataTransition, Enum.GetName(typeof(DataTransition), DataTransition).ToLower());
            }

            if (PrefetchData)
            {
                writer.AddAttribute(MetadataAttribute.DataPrefetch, Boolean.TrueString.ToLower());
            }

            if (DialogBox != DialogBox.None)
            {
                switch (DialogBox)
                {
                    case DialogBox.Open:
                        writer.AddAttribute(MetadataAttribute.DataRel, DataRel.Dialog);
                        break;
                    case DialogBox.Close:
                        writer.AddAttribute(MetadataAttribute.DataRel, DataRel.Back);
                        break;
                }
            }
        }
        #endregion
    }
}
